package com.mobile.framework.util;

import java.security.MessageDigest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StringUtils {

    private final static Log log = LogFactory.getLog(StringUtils.class);

    public static void main(String[] arge) {

    }

    public static String UnicodeToString1(String arges) {
        String value = "";
        String[] valueArray = arges.split("#u");
        for (int i = 1; i < valueArray.length; i++) {
            String valueA = valueArray[i];
            char c = (char) Integer.valueOf(valueA, 16).intValue();
            value += String.valueOf(c);
        }
        return value;
    }

    public static String StringToUnicode1(String args) {
        int len = args.length();
        String[] s = new String[len];
        String value = "";
        for (int i = 0; i < len; i++) {
            char c = args.charAt(i);
            s[i] = Integer.toString(c, 16);
            value += "#u" + s[i];
        }
        return value;
    }

    public static String UnicodeToString(String arges) {
        String value = "";
        String[] valueArray = arges.split("\\\\u");
        for (int i = 1; i < valueArray.length; i++) {
            String valueA = valueArray[i];
            char c = (char) Integer.valueOf(valueA, 16).intValue();
            value += String.valueOf(c);
        }
        return value;
    }

    public static String StringToUnicode(String args) {
        int len = args.length();
        String[] s = new String[len];
        String value = "";
        for (int i = 0; i < len; i++) {
            char c = args.charAt(i);
            s[i] = Integer.toString(c, 16);
            value += "\\u" + s[i];
        }
        return value;
    }

    public static String toUnicode(java.lang.String text) {
        if (text == null)
            return "";
        char chars[] = text.toCharArray();
        java.lang.StringBuffer sb = new StringBuffer();
        int length = chars.length;
        for (int i = 0; i < length; i++) {
            int s = chars[i];
            sb.append("&#");
            sb.append(s);
            sb.append(";");
        }

        return sb.toString();
    }


    public static String encodePassword(String password, String algorithm) {
        byte[] unencodedPassword = password.getBytes();

        MessageDigest md = null;

        try {
            // first create an instance, given the provider
            md = MessageDigest.getInstance(algorithm);
        } catch (Exception e) {
            log.error("Exception: " + e);

            return password;
        }

        md.reset();

        // call the update method one or more times
        // (useful when you don't know the size of your data, eg. stream)
        md.update(unencodedPassword);

        // now calculate the hash
        byte[] encodedPassword = md.digest();

        StringBuffer buf = new StringBuffer();

        for (byte anEncodedPassword : encodedPassword) {
            if ((anEncodedPassword & 0xff) < 0x10) {
                buf.append("0");
            }

            buf.append(Long.toString(anEncodedPassword & 0xff, 16));
        }

        return buf.toString();
    }

}